package com.htudata.zabbix.item.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.htudata.core.util.operation.ArrayOperationUtils;
import com.htudata.core.util.operation.CollectionsOperationUtils;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.zabbix.api.service.ItemZabbixApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ZBXItemService {

  public static final Map<Integer, String> ZBX_ITEM_VALUE_TYPE_MAP = (Map<Integer, String>)new Object();
  
  @Autowired
  private ItemZabbixApiService itemZabbixApiService;
  
  public static boolean isValidZBXItemId(String id) {
    if (StringOperationUtils.isPositiveInt(id))
      return true; 
    return false;
  }
  
  public static String getValueTypeName(int valueType) {
    String result = ZBX_ITEM_VALUE_TYPE_MAP.get(Integer.valueOf(valueType));
    if (!StringOperationUtils.isValidStr2(result))
      return ""; 
    return result;
  }
  
  public JSONArray getZBXItems(String[] groupIdArr, String[] hostIdArr, String[] templateIdArr, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("sortfield", "itemid");
    params.put("sortorder", "DESC");
    params.put("output", new String[] { "itemid", "name", "key_", "type", "history", "trends", "delay", "status", "value_type", "error" });
    if (ArrayOperationUtils.isValidArr((Object[])groupIdArr))
      params.put("groupids", groupIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])hostIdArr))
      params.put("hostids", hostIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])templateIdArr))
      params.put("templateids", templateIdArr); 
    params.put("selectHosts", new String[] { "host", "name" });
    params.put("selectTriggers", "count");
    String json = this.itemZabbixApiService.getItems(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
  
  public JSONArray getZBXItems2(String[] groupIdArr, String[] hostIdArr, String[] templateIdArr, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("sortfield", "itemid");
    params.put("sortorder", "DESC");
    params.put("output", new String[] { "itemid", "name" });
    if (ArrayOperationUtils.isValidArr((Object[])groupIdArr))
      params.put("groupids", groupIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])hostIdArr))
      params.put("hostids", hostIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])templateIdArr))
      params.put("templateids", templateIdArr); 
    String json = this.itemZabbixApiService.getItems(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
  
  public JSONObject getZBXItems3(String[] groupIdArr, String[] hostIdArr, String[] itemIdArr, JSONArray templateIds, int limit) throws Exception {
    JSONObject params = new JSONObject();
    params.put("preservekeys", Boolean.valueOf(true));
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("output", new String[] { "itemid", "name", "description", "value_type", "units" });
    if (ArrayOperationUtils.isValidArr((Object[])groupIdArr))
      params.put("groupids", groupIdArr); 
    if (ArrayOperationUtils.isValidArr((Object[])hostIdArr))
      params.put("hostids", hostIdArr); 
    if (CollectionsOperationUtils.isValidCollection((Collection)templateIds))
      params.put("templateids", templateIds); 
    if (ArrayOperationUtils.isValidArr((Object[])itemIdArr))
      params.put("itemids", itemIdArr); 
    params.put("selectTriggers", new String[] { "priority" });
    String json = this.itemZabbixApiService.getItems(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    try {
      JSONObject result = root.getJSONObject("result");
      return result;
    } catch (Exception e) {
       throw e;
    } 
  }
  
  public JSONArray getZBXItemInfos(Long[] itemIdArr, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("sortfield", "itemid");
    params.put("sortorder", "DESC");
    params.put("output", new String[] { "itemid", "name", "key_", "type", "history", "trends", "delay", "status", "value_type", "description" });
    if (ArrayOperationUtils.isValidArr((Object[])itemIdArr))
      params.put("itemids", itemIdArr); 
    params.put("selectInterfaces", new String[] { "interfaceid" });
    String json = this.itemZabbixApiService.getItems(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
  
  public <T> JSONArray getZBXItemIds(List<T> triggerIdList, int limit) throws Exception {
    JSONObject params = new JSONObject();
    if (limit > 0)
      params.put("limit", Integer.valueOf(limit)); 
    params.put("output", new String[] { "itemid" });
    if (CollectionsOperationUtils.isValidCollection(triggerIdList))
      params.put("triggerids", triggerIdList); 
    String json = this.itemZabbixApiService.getItems(params);
    JSONObject root = JSON.parseObject(json);
    if (root == null)
      return null; 
    JSONArray result = root.getJSONArray("result");
    return result;
  }
}
